package com.feng.leetcode;

public class Twelfth {
    public static void main(String[] args) {
        System.out.println(intToRoman(19));
    }

    public static String intToRoman(int num) {
        String teshu = teshu(num);
        if (!teshu.equals("")){
            return teshu;
        }
        String ans = "";
        if (num/1000 > 0){
            int first = num/1000;
            for (int i = 0; i < first; i++) {
                ans += "M";
            }
            num = num % 1000;
        }
        if (num/500 > 0){
            int deal = num - num%100;
            String teshu1 = teshu(deal);
            if (!teshu1.equals("")){
                ans += teshu1;
                num = num - deal;
            }else {
                int second = num/500;
                for (int i = 0; i < second; i++) {
                    ans += "D";
                }
                num = num % 500;
            }
        }
        if (num/100 > 0){
            int deal = num - num%100;
            String teshu1 = teshu(deal);
            if (!teshu1.equals("")){
                ans += teshu1;
                num = num - deal;
            }else{
                int third = num/100;
                for (int i = 0; i < third; i++) {
                    ans += "C";
                }
                num = num % 100;
            }
        }
        if (num/50 > 0){
            int deal = num - num%10;
            String teshu1 = teshu(deal);
            if (!teshu1.equals("")){
                ans += teshu1;
                num = num - deal;
            }else{
                int fourth = num/50;
                for (int i = 0; i < fourth; i++) {
                    ans += "L";
                }
                num = num % 50;
            }
        }
        if (num/10 > 0){
            int deal = num - num%10;
            String teshu1 = teshu(deal);
            if (!teshu1.equals("")){
                ans += teshu1;
                num = num - deal;
            }else{
                int fifth = num/10;
                for (int i = 0; i < fifth; i++) {
                    ans += "X";
                }
                num = num % 10;
            }

        }
        if (num/5 > 0){
            String teshu1 = teshu(num);
            if (!teshu1.equals("")){
                ans += teshu1;
                num = 0;
            }else{
                int six = num/5;
                for (int i = 0; i < six; i++) {
                    ans += "V";
                }
                num = num % 5;
            }
        }
        if (num > 0){
            String teshu1 = teshu(num);
            if (!teshu1.equals("")){
                ans += teshu1;
            }else{
                int seventh = num;
                for (int i = 0; i < seventh; i++) {
                    ans += "I";
                }
            }

        }

        return ans;
    }

    public static String teshu(int num){
        if (num == 4)
            return "IV";
        if (num == 9)
            return "IX";
        if (num == 40)
            return "XL";
        if (num == 90)
            return "XC";
        if (num == 400)
            return "CD";
        if (num == 900)
            return "CM";
        return "";
    }
}
